unit oFormDocasneVykreslovanieNaGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, oCellularMotion, oLevel, Grids, oSpawn, oPhysicalEntity, oCoordinates,
  oSink, StdCtrls, ComCtrls, ExtCtrls, oPedestrian, uOspList, oCell, ToolWin, oLogicalEntity,
  oEntityTypeConstants, oPolygon, oEntity, oPathFinding, oMatrixCreating, oCellularWorld;

const
  BlueStart = 1;
  GreenStart = 2;
  BlueGoal = 3;
  GreenGoal = 4;
  Barrier = 5;
  c = 4;
  c2 = 15;
  stupen = $00000001;
  pismo = 6;

type
  tNastroj = (start, ciel, chodec, bariera, guma);
  tFarba = (modra, zelena, cervena);

  TDocasny = class(TForm)
    TrackBar1: TTrackBar;
    pSimulacia: TPanel;
    btnStart: TButton;
    btnStop: TButton;
    pPedestrians: TPanel;
    Label2: TLabel;
    lCrowd: TLabel;
    tbCrowd: TTrackBar;
    btnResetCrowd: TButton;
    Label5: TLabel;
    lBarrier: TLabel;
    tbBarrier: TTrackBar;
    btnResetBarrier: TButton;
    chbDiagonal: TCheckBox;
    chbRearward: TCheckBox;
    leGainTightning: TLabeledEdit;
    UpDown1: TUpDown;
    PageControl: TPageControl;
    tabBlue: TTabSheet;
    tabGreen: TTabSheet;
    sgBlueway: TStringGrid;
    sgBluematrix: TStringGrid;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton10: TToolButton;
    Label1: TLabel;
    lFarba: TLabel;
    Label3: TLabel;
    lNastroj: TLabel;
    Button3: TButton;
    Grid: TStringGrid;
    Button4: TButton;
    Button5: TButton;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    sgGreenway: TStringGrid;
    sgGreenMatrix: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure tbCrowdChange(Sender: TObject);
    procedure tbBarrierChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnInsertPedestriansClick(Sender: TObject);
    procedure btnResetCrowdClick(Sender: TObject);
    procedure btnResetBarrierClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure GridSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure GridDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button4Click(Sender: TObject);
    procedure sgBluewayDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure sgBluematrixDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure sgGreenwayDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure sgGreenMatrixDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button5Click(Sender: TObject);
  private
    aWorld : tLevel;
    MS : boolean;
    ZS : boolean;
    MC : boolean;
    ZC : boolean;
    X : integer;
    Y : integer;
    rX : real;
    rY : real;
    Zobrazovanie : word;
  public
    procedure ShowWorld(paWorld : tLevel);
    procedure SetLabel(paTB : tTrackBar; paL : tLabel);
    procedure VytvorCellularWorld(paWorld : tLevel);
    procedure UkazWorld(paWorld : tCellularWorld);
  end;

var
  Docasny: TDocasny;
  Nastroj : tNastroj;
  Farba : tFarba;

  ModryCiel : tSink;
  ZelenyCiel : tSink;

  surMS : tPoint;
  surZS : tPoint;
  surMC : tPoint;
  surZC : tPoint;

  ModraMatica : fMatrix;
  ZelenaMatica : fMatrix;

  Podklad : fMatrix;

implementation

{$R *.dfm}

procedure tDocasny.UkazWorld(paWorld : tCellularWorld);
begin
  Zobrazovanie := 2;
  Svet := tLevel.Create;
  Svet.CellWorld := paWorld;
  {rX := paWorld.SizeX;
  rY := paWorld.SizeY;  }
  X := trunc(paWorld.Columns);
  Y := trunc(paWorld.Rows);
  Grid.ColCount := X;
  Grid.RowCount := Y;
  ShowModal;
  Zobrazovanie := 1;
end;

procedure tDocasny.VytvorCellularWorld(paWorld : tLevel);
  var i, j : integer;
     list : tList;
     Position : tCoordinates;
     polygon : tPolygon;
begin

  for I := 0 to length(Podklad) - 1 do
    for j := 0 to length(Podklad[i]) - 1 do begin
      case Podklad[i, j] of
        BlueGoal : begin
          Position := getCentreOfCell(i,j);
          list := tList.Create;
          list.Add(TCoordinates.Create(position.X - c2 , position.Y + c2));
          list.Add(TCoordinates.Create(position.X - c2 , position.Y - c2));
          list.Add(TCoordinates.Create(position.X + c2 , position.Y - c2));
          list.Add(TCoordinates.Create(position.X + c2 , position.Y + c2));
          list.Add(TCoordinates.Create(position.X - c2 , position.Y + c2));
          polygon := Tpolygon.Create(list, 100);
          ModryCiel := tSink(paWorld.insertLogicalEntity(100, ET_SINK_TYPE, polygon, 0));
          ModryCiel.farba := 1;
        end;
        GreenGoal : begin
          Position := getCentreOfCell(i,j);
          list := tList.Create;
          list.Add(TCoordinates.Create(position.X - c2 , position.Y + c2));
          list.Add(TCoordinates.Create(position.X - c2 , position.Y - c2));
          list.Add(TCoordinates.Create(position.X + c2 , position.Y - c2));
          list.Add(TCoordinates.Create(position.X + c2 , position.Y + c2));
          list.Add(TCoordinates.Create(position.X - c2 , position.Y + c2));
          polygon := Tpolygon.Create(list, 100);
          ZelenyCiel := tSink(paWorld.insertLogicalEntity(200, ET_SINK_TYPE, polygon, 0));
          ZelenyCiel.farba := 2;
        end;
        Barrier : begin
          Position := getCentreOfCell(i,j);
          insertBarrier(paWorld,1,position.X - c, position.Y - c, position.X + c, position.Y - c, position.X - c, position.Y + c, position.X + c, position.Y + c);
        end;
      end;
    end;
  paWorld.getCellularWorld(10, 10, 1);
  Hodnota := 1;
  ModraMatica := CreateMatrix(aWorld.CellWorld, aWorld.CellWorld.Cells[surMS.X, surMS.Y], aWorld.CellWorld.Cells[surMC.X, surMC.Y]);
  Hodnota := 2;
  ZelenaMatica := CreateMatrix(aWorld.CellWorld, aWorld.CellWorld.Cells[surZS.X, surZS.Y], aWorld.CellWorld.Cells[surZC.X, surZC.Y]);
  for I := 0 to length(Podklad) - 1 do
    for j := 0 to length(Podklad[i]) - 1 do begin
      case Podklad[i, j] of
        BlueStart : begin
          Position := getCentreOfCell(i,j);
          InsertPedestrians(chbDiagonal.Checked, strtofloat(lCrowd.Caption), strtofloat(lBarrier.Caption),strtoint(leGainTightning.Text), aWorld, 1, Position, ModryCiel, ModraMatica);
        end;
        GreenStart : begin
          Position := getCentreOfCell(i,j);
          InsertPedestrians(chbDiagonal.Checked, strtofloat(lCrowd.Caption), strtofloat(lBarrier.Caption),strtoint(leGainTightning.Text), aWorld, 2, Position, ZelenyCiel, ZelenaMatica);
        end;
      end;
    end;
end;

procedure tDocasny.SetLabel(paTB : tTrackBar; paL : tLabel);
  var pom : real;
begin
  if paTb.Position < 500  then begin
    pom := 500 - paTB.Position;
    pom := pom / 100;
    pom := - pom;
  end

  else if paTB.Position > 500 then begin
    pom := paTb.Position - 500;
    pom := pom / 100;
  end

  else begin
    pom := 0;
  end;

  paL.Caption := floattostr(pom);
end;

procedure TDocasny.sgBluematrixDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if length(ModraMatica) <> 0 then begin
    if ModraMatica[aCol, aRow] = MaxInt then begin
      sgBluematrix.Canvas.Brush.Color := clBlack;
      sgBluematrix.Canvas.TextRect(rect,rect.Left,rect.top,'')
    end
    else begin
      sgBluematrix.Canvas.Font.Size := pismo;
      sgBluematrix.Canvas.Brush.Color := clYellow  - (ModraMatica[aCol, aRow] * stupen);
      sgBluematrix.Canvas.TextRect(rect,rect.Left,rect.top,inttostr(ModraMatica[aCol, aRow]));
    end;
    sgBluematrix.Canvas.FrameRect(rect);
  end;
end;

procedure TDocasny.sgBluewayDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
    if Svet.CellWorld <> nil then
    with Svet.CellWorld.Cells[aCol,aRow] do begin
      if IsBarrier then begin  // ak je to bariera
        sgBlueway.Canvas.Brush.Color := clBlack;
        sgBlueway.Canvas.TextRect(rect,rect.Left,rect.top,'');
        sgBlueway.Canvas.FrameRect(rect);
      end;
      if (Way = 1) or (Way = 3) then begin
        sgBlueway.Canvas.Brush.Color := clRed;
        sgBlueway.Canvas.TextRect(rect,rect.Left,rect.top,'');
        sgBlueway.Canvas.FrameRect(rect);
      end;
    end;
end;

procedure TDocasny.sgGreenMatrixDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if length(ZelenaMatica) <> 0 then begin
    if ZelenaMatica[aCol, aRow] = MaxInt then begin
      sgGreenmatrix.Canvas.Brush.Color := clBlack;
      sgGreenmatrix.Canvas.TextRect(rect,rect.Left,rect.top,'')
    end
    else begin
      sgGreenmatrix.Canvas.Font.Size := pismo;
      sgGreenmatrix.Canvas.Brush.Color := clYellow - (ZelenaMatica[aCol, aRow] * stupen);
      sgGreenmatrix.Canvas.TextRect(rect,rect.Left,rect.top,inttostr(ZelenaMatica[aCol, aRow]));
    end;
    sgGreenmatrix.Canvas.FrameRect(rect);
  end;
end;

procedure TDocasny.sgGreenwayDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if Svet.CellWorld <> nil then
    with Svet.CellWorld.Cells[aCol,aRow] do begin
      if IsBarrier then begin  // ak je to bariera
        sgGreenway.Canvas.Brush.Color := clBlack;
        sgGreenway.Canvas.TextRect(rect,rect.Left,rect.top,'');
        sgGreenway.Canvas.FrameRect(rect);
      end;
      if (Way = 2) or (Way = 3) then begin
        sgGreenway.Canvas.Brush.Color := clRed;
        sgGreenway.Canvas.TextRect(rect,rect.Left,rect.top,'');
        sgGreenway.Canvas.FrameRect(rect);
      end;
    end;
end;

procedure tDocasny.ShowWorld(paWorld: TLevel);
  var i, j : integer;
begin
  aWorld := paWorld;
  Zobrazovanie := 1;
  rX := aWorld.SizeX;
  rY := aWorld.SizeY;
  X := trunc(aWorld.SizeX / 10);
  Y := trunc(aWorld.SizeY / 10);
  Grid.ColCount := X;
  Grid.RowCount := Y;
  //Grid.Height := X * (Grid .DefaultRowHeight + 1) + 4;
  //Grid.Width := Y * (Grid .DefaultColWidth + 1) + 4;
  sgBlueway.ColCount := Grid.ColCount;
  sgBlueway.RowCount := Grid.RowCount;
  sgBlueway.Height := Grid.Height;
  sgBlueway.Width := Grid.Width;
  sgBlueway.DefaultColWidth := Grid.DefaultColWidth;
  sgBlueway.DefaultRowHeight := Grid.DefaultRowHeight;
  sgBluematrix.ColCount := Grid.ColCount;
  sgBluematrix.RowCount := Grid.RowCount;
  sgBluematrix.Height := Grid.Height;
  sgBluematrix.Width := Grid.Width;
  sgBluematrix.DefaultColWidth := Grid.DefaultColWidth;
  sgBluematrix.DefaultRowHeight := Grid.DefaultRowHeight;
  sgGreenway.ColCount := Grid.ColCount;
  sgGreenway.RowCount := Grid.RowCount;
  sgGreenway.Height := Grid.Height;
  sgGreenway.Width := Grid.Width;
  sgGreenway.DefaultColWidth := Grid.DefaultColWidth;
  sgGreenway.DefaultRowHeight := Grid.DefaultRowHeight;
  sgGreenmatrix.ColCount := Grid.ColCount;
  sgGreenmatrix.RowCount := Grid.RowCount;
  sgGreenmatrix.Height := Grid.Height;
  sgGreenmatrix.Width := Grid.Width;
  sgGreenmatrix.DefaultColWidth := Grid.DefaultColWidth;
  sgGreenmatrix.DefaultRowHeight := Grid.DefaultRowHeight;
  setlength(Podklad, X);
  for i := 0 to X - 1 do
    setlength(Podklad[i], Y);
  for i := 0 to X - 1 do
    for j := 0 to Y - 1 do
      Podklad[i, j] := 0;
  Show;
end;

procedure TDocasny.btnInsertPedestriansClick(Sender: TObject);
begin
//  InsertPedestrians(chbDiagonal.Checked, strtofloat(lCrowd.Caption), strtofloat(lBarrier.Caption),strtoint(leGainTightning.Text), aWorld, Integer(Farba));
end;

procedure TDocasny.btnResetBarrierClick(Sender: TObject);
begin
  tbBarrier.Position := 500;
end;

procedure TDocasny.btnResetCrowdClick(Sender: TObject);
begin
  tbCrowd.Position := 500;
end;

procedure TDocasny.btnStartClick(Sender: TObject);
begin
  MoveAll;
end;

procedure TDocasny.btnStopClick(Sender: TObject);
begin
  Stop := true;
end;



procedure TDocasny.Button1Click(Sender: TObject);
  var ped : tPedestrian;
begin
  while aWorld.AllPedestrians.Count <> 0 do begin
    ped :=  aWorld.AllPedestrians.Extract(aWorld.AllPedestrians.Items[0]);
    aWorld.deletePedestrian(ped);
  end;
  Grid.Repaint;
end;

procedure TDocasny.Button2Click(Sender: TObject);
  var list : tOspList;
      pom : tCell;
      color : tColor;
begin
  Matica := CreateMatrix(Svet.CellWorld, Svet.CellWorld.Cells[3,3], Svet.CellWorld.Cells[7,0]);
end;

procedure TDocasny.Button3Click(Sender: TObject);
  var i, j : integer;
begin
  for i := 0 to X - 1 do
    for j := 0 to Y - 1 do
      Podklad[i, j] := 0;
  MS := false;
  ZS := false;
  MC := false;
  ZC := false;
  Grid.Repaint;
end;

procedure TDocasny.Button4Click(Sender: TObject);
  var way : tOspList;
begin
  if MS and ZS and MC and ZC then begin
    PageControl.Enabled := true;
    //pPedestrians.Enabled := true;
    pSimulacia.Enabled := true;
    ToolBar1.Enabled := false;
    Zobrazovanie := 2;
    VytvorCellularWorld(aWorld);
    Grid.Repaint;
    sgBlueway.Repaint;
  end
  else Showmessage('Vytvor zelene aj modre starty a ciele!');
end;

procedure TDocasny.Button5Click(Sender: TObject);
  var i,j : integer;
begin
  FreeAndNil(aWorld);
  Svet := tLevel.Create(rx, ry);
  aWorld := Svet;
  PageControl.Enabled := false;
  pSimulacia.Enabled := false;
  //pPedestrians.Enabled := false;
  ToolBar1.Enabled := true;
  for i := 0 to X - 1 do
    for j := 0 to Y - 1 do
      Podklad[i, j] := 0;
  MS := false;
  ZS := false;
  MC := false;
  ZC := false;
  Zobrazovanie := 1;
  Grid.Repaint;
  sgBlueway.Repaint;
  sgBluematrix.Repaint;
  sgGreenMatrix.Repaint;
  sgGreenWay.Repaint;
end;

procedure TDocasny.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Stop := True;
end;

procedure TDocasny.FormCreate(Sender: TObject);
  var phyEnt : TPhysicalEntity;
      coord : tCoordinates;
begin
  SetLabel(tbCrowd, lCrowd);
  SetLabel(tbBarrier, lBarrier);
  MS := false;
  ZS := false;
  MC := false;
  ZC := false;
end;

procedure TDocasny.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Svet);
end;


procedure TDocasny.tbBarrierChange(Sender: TObject);
begin
  SetLabel(tbBarrier, lBarrier);
end;

procedure TDocasny.tbCrowdChange(Sender: TObject);
begin
  SetLabel(tbCrowd, lCrowd);
end;

procedure TDocasny.ToolButton1Click(Sender: TObject);
begin
  lFarba.Font.Color := clBlue;
  lFarba.Caption := 'MODRA';
  Farba := modra;
end;

procedure TDocasny.ToolButton2Click(Sender: TObject);
begin
  lFarba.Font.Color := clGreen;
  lFarba.Caption := 'ZELENA';
  Farba := zelena;
end;

procedure TDocasny.ToolButton4Click(Sender: TObject);
begin
  lNastroj.Caption := 'START';
  Nastroj := start;
end;

procedure TDocasny.ToolButton5Click(Sender: TObject);
begin
  lNastroj.Caption := 'CIEL';
  Nastroj := ciel;
end;

procedure TDocasny.ToolButton6Click(Sender: TObject);
begin
  lNastroj.Caption := 'BARIERA';
  Nastroj := bariera;
end;

procedure TDocasny.ToolButton8Click(Sender: TObject);
begin
  lNastroj.Caption := 'CHODEC';
  Nastroj := chodec;

end;

procedure TDocasny.ToolButton9Click(Sender: TObject);
begin
  lNastroj.Caption := 'GUMA';
  Nastroj := guma;

end;

procedure TDocasny.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
 var pom : tObject;
     S : string;
begin
  if Zobrazovanie = 1 then begin
  S := '';
  Grid.Canvas.Font.Size := 6;
  case Podklad[aCol, aRow] of
    Barrier : Begin
      Grid.Canvas.Brush.Color := clBlack;
    End;
    BlueStart : begin
      Grid.Canvas.Brush.Color := clBlue;
      S := 'S';
    end;
    GreenStart : begin
      Grid.Canvas.Brush.Color := clGreen;
      S := 'S';
    end;
    BlueGoal : begin
      Grid.Canvas.Brush.Color := clBlue;
      S := 'G';
    end;
    GreenGoal : begin
      Grid.Canvas.Brush.Color := clGreen;
      S := 'G';
    end;
  end;
  Grid.Canvas.TextRect(rect, rect.Left, rect.top, S);
  Grid.Canvas.FrameRect(rect);
  end
  else begin
    pom := nil;
    with Svet.CellWorld.Cells[aCol,aRow] do begin
      if Pedestrians.Count = 0 then begin
        if LogEntities.Count <> 0 then
          pom := LogEntities.First;
        if pom <> nil then begin
          if pom is tSink then begin
           if tSink(pom).farba = 1 then
             Grid.Canvas.Brush.Color := clBlue
           else
             Grid.Canvas.Brush.Color := clGreen;
           Grid.Canvas.TextRect(rect,rect.Left,rect.top,'');
           Grid.Canvas.FrameRect(rect);
          end
          else begin // ak je to daka ina picovina
            Grid.Canvas.Brush.Color := clPurple;
            Grid.Canvas.TextRect(rect,rect.Left,rect.top,'');
            Grid.Canvas.FrameRect(rect);
          end;
        end
        else begin
          if IsBarrier then begin  // ak je to bariera
            Grid.Canvas.Brush.Color := clBlack;
            Grid.Canvas.TextRect(rect,rect.Left,rect.top,'');
            Grid.Canvas.FrameRect(rect);
          end;
        end;
      end
      else begin  // ak je to chodec
        pom := tPedestrian(Pedestrians.First);
        if tPedestrian(pom).Typ = 1 then
          Grid.Canvas.Brush.Color := clBlue
        else
          Grid.Canvas.Brush.Color := clGreen;
        Grid.Canvas.Ellipse(rect);
      end;
    end;
  end;
end;

procedure TDocasny.GridSelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
  var coor : tCoordinates;
      list : tList;
      polygon : tPolygon;
begin
  if Podklad[aCol, aRow] = 0 then begin
  case Nastroj of
    start : begin
      if Farba = modra then begin
        if NOT MS then begin
          Podklad[aCol, aRow] := BlueStart;
          MS := true;
          surMS.X := aCol;
          surMS.Y := aRow;
        end
      end
      else begin
        if NOT ZS then begin
          Podklad[aCol, aRow] := GreenStart;
          ZS := true;
          surZS.X := aCol;
          surZS.Y := aRow;
        end;
      end;
    end;
    ciel : begin
      if Farba = modra then begin
        if NOT MC then begin
          Podklad[aCol, aRow] := BlueGoal;
          MC := True;
          surMC.X := aCol;
          surMC.Y := aRow;
        end
      end
      else begin
        if NOT ZC then begin
          Podklad[aCol, aRow] := GreenGoal;
          ZC := true;
          surZC.X := aCol;
          surZC.Y := aRow;
        end;
      end;
    end;
    bariera : begin
      Podklad[aCol, aRow] := Barrier;
    end;
  end;
  Grid.Repaint;
  end;
end;

end.



